package com.cruisecontrol4eclipse.core;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

import com.cruisecontrol4eclipse.core.model.CruiseControlProject;
import com.cruisecontrol4eclipse.core.model.CruiseControlProjectColor;
import com.cruisecontrol4eclipse.core.model.CruiseControlProject.Status;
import com.cruisecontrol4eclipse.core.model.CruiseControlProjectColor.ProjectColor;

public class CruiseControlOverviewTableLabelProvider implements
		ITableLabelProvider {
	private Image red;
	private Image green;
	private Image yellow;

	public CruiseControlOverviewTableLabelProvider() {
		green = Activator.imageDescriptorFromPlugin(
				"com.cruisecontrol4eclipse.core", "resources/green.png")
				.createImage();
		red = Activator.imageDescriptorFromPlugin(
				"com.cruisecontrol4eclipse.core", "resources/red.png")
				.createImage();
		yellow = Activator.imageDescriptorFromPlugin(
				"com.cruisecontrol4eclipse.core", "resources/yellow.png")
				.createImage();
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public void dispose() {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		CruiseControlProject project = ((CruiseControlProject) element);
		if (columnIndex == 0) { 
			ProjectColor color = CruiseControlProjectColor.getColor(project);
			if(ProjectColor.YELLOW.equals(color)) {
				return yellow;
			} else  if(project.getLabel() != null && project.getLabel().equals(project.getLastSuccessfulBuildLabel())) {
				return green;
			} else if(project.getLabel() != null){
				return red;	
			}
		}
		return null;
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		CruiseControlProject project = ((CruiseControlProject) element);
		switch (columnIndex) {
		case 0:
			return "" + project.getName();
		case 1:
			return "" + project.getLabel();
		case 2:
			return project.getStatus() == null ? "unknown" : project.getStatus().toString();
		case 3:
			return project.getLastSuccessfulBuildLabel() == null ? "None" : project.getLastSuccessfulBuildLabel();
		case 4:
			return project.getBuildStartTime() == null ? ""
					: project.getBuildStartTime()
							.toString();
		}
		return "";
	}
}
